package Manager;

import CODEC.Decoding;
import CODEC.Impl.DecodingImpl;
import domain.Message;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @Author: ylc
 * @Description:接收数据包
 */
public class UDPServer {
    public static void main(String[] args) throws IOException {
        Decoding decoder = new DecodingImpl();
        // 创建服务端DatagramSocket，指定端口，可以发送及接收UDP数据报
        DatagramSocket socket = new DatagramSocket(9999);
        while (true) {
            // 创建DatagramPacket数据包用于接收数据
            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
            // 接收数据
            socket.receive(packet);
            // 解析SNMP数据包
            byte[] snmpPacket = packet.getData();
            Message message = decoder.getSnmpMessageValue(snmpPacket);
            System.out.println(message);
        }
    }
}

